package com.kaka.bluetoothble.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kaka.bluetoothble.R;
import com.yunlin.bean.BleDevice;

import java.util.List;

public class BleAdapter extends YLBaseAdapter<BleDevice>{
    private Context mContext;
    private List<BleDevice> mBluetoothDevices;
    
    public BleAdapter(Context mContext, List<BleDevice> listBleDevices){
        super(mContext, listBleDevices);
        this.mContext=mContext;
        this.mBluetoothDevices=listBleDevices;
    }
    
    public void addAllData(List<BleDevice> scanDevices){
        mBluetoothDevices.clear();
        mBluetoothDevices.addAll(scanDevices);
        notifyDataSetChanged();
    }
    
    @Override
    public int getCount(){
        return mBluetoothDevices.size();
    }
    
    @Override
    public BleDevice getItem(int position){
        return mBluetoothDevices.get(position);
    }
    
    @Override
    public long getItemId(int position){
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView=LayoutInflater.from(mContext).inflate(R.layout.bluetooth_devices_list_item, null);
            viewHolder=new ViewHolder();
            viewHolder.tvDeviceName=convertView.findViewById(R.id.tvDeviceName);
            viewHolder.tvMacAddress=convertView.findViewById(R.id.tvMacAddress);
            viewHolder.tvK0=convertView.findViewById(R.id.tvK0);
            viewHolder.tvK1=convertView.findViewById(R.id.tvK1);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        BleDevice device=getItem(position);
        viewHolder.tvDeviceName.setText(device.getDeviceName());
        viewHolder.tvMacAddress.setText(device.getMacAddress());
        viewHolder.tvK0.setText(device.getK0());
        viewHolder.tvK1.setText(device.getK1());
        return convertView;
    }
    
    
    static final class ViewHolder{
        public TextView tvDeviceName;
        public TextView tvMacAddress;
        public TextView tvK0;
        public TextView tvK1;
        
    }
}
